{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 编码格式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好\n"
     ]
    }
   ],
   "source": [
    "# 默认：UTF-8\n",
    "# 修改：最前面添加encoding=GBK等，不同的编码方式决定了占磁盘字节是多少\n",
    "print('你好')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 读取磁盘文件内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['你好呀\\n', '美丽']\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "文件的读写原理：IO操作\n",
    "队列先进先出\n",
    ".py文件到解释器，到os，再到硬盘中操作\n",
    "步骤：打开或新建文件，读写文件，关闭资源\n",
    "\"\"\"\n",
    "file = open('a.txt', 'r')  # r表示读取\n",
    "print(file.readlines())  # 结果是个列表，所有内容行\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 常用文件打开模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r只读模式\n",
    "\n",
    "# w为只写模式，不存在则创建，存在则覆盖原有内容\n",
    "file = open('b.txt', 'w')\n",
    "file.write('Python')  # 用write进行替换原有内容\n",
    "file.close()\n",
    "\n",
    "# a追加模式，不存在则创建，指针在文件开头，文件存在，在文件末尾追加内容，指针在文件末尾\n",
    "file = open('b.txt', 'a')\n",
    "file.write('Python')\n",
    "file.close()\n",
    "\n",
    "# b以二进制方式打开文件，不能单独使用，需要与其他模式一起使用，rb或者rw\n",
    "src_file = open('xiaowangtongxue_logo.jpg', 'rb')\n",
    "target_file = open('copy_xiaowangtongxue_logo.jpg', 'wb')\n",
    "target_file.write(src_file.read())  # 边读边写\n",
    "src_file.close()\n",
    "\n",
    "# +以读写模式打开，不能单独使用，需要与其他模式一起使用，a+"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 文件对象常用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好呀\n",
      "美丽\n",
      "\n",
      "\n",
      "[]\n",
      "好呀\n",
      "美丽\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "file = open('a.txt', 'r')\n",
    "print(file.read())  # 读取全部内容\n",
    "print(file.read(2))  # 读取两个字符\n",
    "\n",
    "print(file.readline())  # 读取一行\n",
    "print(file.readlines())  # 每一行作为独立的字符串对象，最后放入列表返回\n",
    "\n",
    "file.seek(2)  # 改变指针，到两个字节后，也就是一个字\n",
    "print(file.read())\n",
    "print(file.tell())  # 返回文件指针的当前位置\n",
    "file.close()\n",
    "\n",
    "file1 = open('c.txt', 'a')\n",
    "lst = ['java', 'go', 'python']\n",
    "file1.write('中国')  # 将字符串内容写入文件\n",
    "file1.writelines(lst)  # 将列表内容写入文件\n",
    "file1.close()\n",
    "\n",
    "# flush()把缓冲区的内容写入文件，但不关闭文件\n",
    "# close()把缓冲区的内容写入文件，关闭文件，释放文件对象相关资源\n",
    "file2 = open('c.txt', 'a')\n",
    "file2.write('hello')\n",
    "file2.flush()\n",
    "file2.write('world')\n",
    "file2.close()  # flush之后还可以写，close之后不可以写，两者换位置以后，不能写入world"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. with语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class '_io.TextIOWrapper'>\n",
      "你好呀\n",
      "美丽\n",
      "enter方法调用执行了\n",
      "show方法调用执行了\n",
      "exit方法调用执行了\n"
     ]
    }
   ],
   "source": [
    "# with语句可以自动管理上下文资源，不管什么原因跳出with块，都能确保文件正常关闭，以此来达到释放资源的目的\n",
    "print(type(open('a.txt', 'r')))  # 上下文管理器\n",
    "with open('a.txt', 'r') as file:\n",
    "    print(file.read())  # 离开with语句自动释放，不用close()\n",
    "\n",
    "\"\"\"\n",
    "MyContentMgr实现了特殊方法enter和exit称为该类对象遵守了上下文管理器协议\n",
    "该诶对象的实例对象称为上下文管理器\n",
    "MyContentMgr()为实例对象\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "class MyContentMgr(object):\n",
    "    def __enter__(self):\n",
    "        print('enter方法调用执行了')\n",
    "        return self\n",
    "\n",
    "    def __exit__(self, exc_type, exc_val, exc_tb):\n",
    "        print('exit方法调用执行了')\n",
    "\n",
    "    def show(self):\n",
    "        print('show方法调用执行了')\n",
    "#         print('show方法调用执行了', 1/0)\n",
    "\n",
    "\n",
    "with MyContentMgr() as file:  # 相当于file=MyContentMgr()\n",
    "    file.show()  # 产生异常也调用了exit方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('xiaowangtongxue_logo.jpg', 'rb') as src_file:\n",
    "    with open('copy2_xiaowangtongxue_logo.jpg', 'wb') as target_file:\n",
    "        target_file.write(src_file.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 6. os模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\wangy\\Desktop\\Python\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# os模块是与操作系统相关的一个模块\n",
    "import os\n",
    "os.system('notepad.exe')\n",
    "os.system('calc.exe')\n",
    "# 直接调用可执行文件\n",
    "os.startfile('xiaowangtongxue.jpg')\n",
    "\n",
    "# 返回当前的工作目录\n",
    "print(os.getcwd())\n",
    "# 返回指定路径下的文件和目录信息\n",
    "lst = os.listdir('related_data')\n",
    "print(lst)\n",
    "# 创建目录\n",
    "os.mkdir('related_data/new_dir')\n",
    "# 创建多级目录\n",
    "os.makedirs('related_data/A/B/C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除目录\n",
    "os.rmdir('related_data/new_dir')\n",
    "os.removedirs('related_data/A/B/C')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.mkdir('related_data') # 创建目录\n",
    "os.mkdir('related_data/new_dir') # 创建目录\n",
    "# os.chdir('related_data') # 将path设置为当前工作目录\n",
    "# print(os.getcwd())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\wangy\\Desktop\\Python\\with语句.py\n",
      "True False\n",
      "D:\\Notebook\\python\\with语句.py\n",
      "('D:\\\\Notebook\\\\python', 'with语句.py')\n",
      "('with语句', '.py')\n",
      "with语句.py\n",
      "D:\\Notebook\\python\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "import os.path\n",
    "# 获取文件或目录的绝对路径\n",
    "print(os.path.abspath('with语句.py'))  # 文件的绝对路径\n",
    "# 用于判断文件或目录是否存在，存在返回True，否则False\n",
    "print(os.path.exists('with语句.py'), os.path.exists('demo.py'))\n",
    "# 将目录与目录或者文件名拼接起来\n",
    "print(os.path.join('D:\\\\Notebook\\\\python', 'with语句.py'))\n",
    "# 从一个目录中提取文件名\n",
    "print(os.path.split('D:\\\\Notebook\\\\python\\\\with语句.py'))\n",
    "# 分离文件名和扩展名\n",
    "print(os.path.splitext('with语句.py'))\n",
    "# 从一个路径中提取文件名\n",
    "print(os.path.basename('D:\\\\Notebook\\\\python\\\\with语句.py'))\n",
    "# 从一个路径中提取文件路径，不包括文件名\n",
    "print(os.path.dirname('D:\\\\Notebook\\\\python\\\\with语句.py'))\n",
    "# 用于判断是否为路径\n",
    "print(os.path.isdir('D:\\\\Notebook\\\\python\\\\with语句.py'))  # False，最后的是文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "path:  C:\\Users\\wangy\\Desktop\\Python\n",
      "calc.py\n",
      "calc2.py\n",
      "with语句.py\n"
     ]
    }
   ],
   "source": [
    "# 列出指定目录下的所有文件\n",
    "import os\n",
    "path = os.getcwd()\n",
    "print(\"path: \", path)\n",
    "lst = os.listdir(path)\n",
    "for filename in lst:\n",
    "    if filename.endswith('.py'):\n",
    "        print(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\n",
      "C:\\Users\\wangy\\Desktop\\Python\\related_data\n",
      "C:\\Users\\wangy\\Desktop\\Python\\__pycache__\n",
      "C:\\Users\\wangy\\Desktop\\Python\\01_输出函数print.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\02_转义字符与原字符.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\03_二进制与字符编码.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\04_标识符和保留字符.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\05_变量定义使用赋值.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\06_常见的数据类型.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\07_数据类型转换.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\08_Python中注释.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\09_input函数使用.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\10_Python的运算符.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\11_程序的顺序结构.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\12_对象的布尔值.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\13_程序的分支结构.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\14_条件表达式.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\15_pass语句.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\16_range函数使用.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\17_while循环使用.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\18_for_in循环使用.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\19_流程控制语句.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\20_else语句.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\21_嵌套循环.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\22_二重循环break与continue.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\23_列表.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\24_字典.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\25_可变不可变序列和元组.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\26_集合.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\27_字符串.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\28_函数.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\29_异常与调试.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\30_类对象与浅深拷贝.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\31_模块与包.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\32_文件读写.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\a.txt\n",
      "C:\\Users\\wangy\\Desktop\\Python\\b.txt\n",
      "C:\\Users\\wangy\\Desktop\\Python\\c.txt\n",
      "C:\\Users\\wangy\\Desktop\\Python\\calc.py\n",
      "C:\\Users\\wangy\\Desktop\\Python\\calc2.py\n",
      "C:\\Users\\wangy\\Desktop\\Python\\copy2_xiaowangtongxue_logo.jpg\n",
      "C:\\Users\\wangy\\Desktop\\Python\\copy_xiaowangtongxue_logo.jpg\n",
      "C:\\Users\\wangy\\Desktop\\Python\\with语句.py\n",
      "C:\\Users\\wangy\\Desktop\\Python\\xiaowangtongxue.jpg\n",
      "C:\\Users\\wangy\\Desktop\\Python\\xiaowangtongxue_logo.jpg\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\01_print打印-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\01_输出函数print-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\01_配置版本 - 副本-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\01_配置版本-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\02_转义字符-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\02_转义字符与原字符-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\03_二进制与字符编码-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\04_标识符和保留字符-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\05_变量定义使用赋值-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\05_变量的定义和使用-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\06_常见的数据类型-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\07_数据类型转换-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\08_Python中注释-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\09_input函数使用-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\10_Python的运算符-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\10_运算符-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\11_程序的组织结构-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\11_程序的顺序结构-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\12_对象的布尔值-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\13_程序的分支结构-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\14_条件表达式-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\15_pass语句-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\16_range函数使用-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\17_while循环-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\17_while循环使用-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\18_for_in循环使用-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\19_流程控制语句-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\20_else语句-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\21_嵌套循环-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\22_二重循环break与continue-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\23_列表-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\24_字典-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\25_元组-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\25_可变不可变序列和元组-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\25_集合-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\26_字符串-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\26_集合-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\27_字符串-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\28_函数-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\29_异常与调试-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\30_类与对象-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\30_类对象与浅深拷贝-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\31_模块与包-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\31_浅拷贝与深拷贝-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\32_文件读写-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\32_模块与包-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\33_文件读写-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\.ipynb_checkpoints\\33_遍历文件-checkpoint.ipynb\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\01_Python编辑器推荐.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\02_Jupyter Notebook安装.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\03_Jupyter Notebook代码文件.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\04_Jupyter Notebook快捷键.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\05_Jupyter Notebook美化.ipynb\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\\01_Python编辑器推荐-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\\02_Jupyter Notebook安装-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\\03_Jupyter Notebook代码文件-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\\04_Jupyter Notebook快捷键-checkpoint.ipynb\n",
      "C:\\Users\\wangy\\Desktop\\Python\\00_Python编辑器\\.ipynb_checkpoints\\05_Jupyter Notebook美化-checkpoint.ipynb\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\__pycache__\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\module_A.py\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\module_B.py\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\__init__.py\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\__pycache__\\module_A.cpython-36.pyc\n",
      "C:\\Users\\wangy\\Desktop\\Python\\pageage1\\__pycache__\\__init__.cpython-36.pyc\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\related_data\\new_dir\n",
      "-------------------\n",
      "-------------------\n",
      "C:\\Users\\wangy\\Desktop\\Python\\__pycache__\\calc.cpython-36.pyc\n",
      "C:\\Users\\wangy\\Desktop\\Python\\__pycache__\\calc2.cpython-36.pyc\n",
      "-------------------\n"
     ]
    }
   ],
   "source": [
    "# 递归遍历目录下所有文件\n",
    "import os\n",
    "path = os.getcwd()\n",
    "lst_files = os.walk(path)\n",
    "for dirpath, dirname, filename in lst_files:\n",
    "    \"\"\"print(dirpath)\n",
    "    print(dirname)\n",
    "    print(filename)\n",
    "    print('-----------')\"\"\"\n",
    "    for dir in dirname:\n",
    "        print(os.path.join(dirpath, dir))\n",
    "    for file in filename:\n",
    "        print(os.path.join(dirpath, file))\n",
    "    print('-------------------')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
